package simi;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class RTag extends BodyTagSupport {

	private boolean silent = false;

	public String getSilent() {
		return (new Boolean(silent).toString());
	}

	public void setSilent(String newSilent) {
		this.silent = Boolean.parseBoolean(newSilent);
	}

	public int doEndTag() throws JspException {

		try {
			String bodyText = bodyContent.getString();
			ExpressionEvaluator ree = new ExpressionEvaluator();
			HttpSession theSession = pageContext.getSession();
			InputStream is = (InputStream) theSession
					.getAttribute(Constants.SESSION_INPUT_STREAM);
			OutputStream os = (OutputStream) theSession
					.getAttribute(Constants.SESSION_OUTPUT_STREAM);
			ByteArrayOutputStream bos = (ByteArrayOutputStream) theSession
					.getAttribute(Constants.RUNNING_OUTPUT_PARAM);

			os.flush();

			ree.setRInputStream(is);
			ree.setROutputStream(os);
			ree.setROutput(bos);

			String result = ree.rEval(bodyText);

			if (!silent)
				pageContext.getOut().write(result);
		} catch (IOException e) {
		}
		return EVAL_PAGE;
	}
}
